package nc.tb.report.plugin;

import java.awt.Component;

import nc.tb.app.CommonPlan;
import nc.tb.app.version.ui.MutiRowCellRenderer;
import nc.tb.cube.CubeView;
import nc.tb.cube.cell.MutiRowCellValueObject;
import nc.tb.cube.model.MetaPlan;
import nc.tb.cube.model.Plan;
import nc.tb.ufo.ui.IMode;

import com.ufsoft.report.UfoReport;
import com.ufsoft.report.command.UfoCommand;
import com.ufsoft.report.toolbar.CombineCellToolBarExt;

public class TbCombineCellToolBarExt extends CombineCellToolBarExt {

	public TbCombineCellToolBarExt(UfoReport report) {
		super(report);
	}
	
	public UfoCommand getCommand() {
	    return new TbCombineCellToolBarCmd();
	}
	
	public boolean isEnabled(Component focusComp) {
		TbReport m_report = (TbReport)getReport();
		if (isVersionStatus(m_report))
			return false;
		boolean isVersionContract = false;
		try {
			isVersionContract = m_report.getTable().getReanderAndEditor().getRender(MutiRowCellValueObject.class) instanceof MutiRowCellRenderer;
		} catch (Exception e) {
		}
		if (isVersionContract)
			return false;
		try {
			Plan plan = m_report.getViewUIModel().getPlan();
			CubeView cv = m_report.getViewUIModel().getCubeView();
			if (plan instanceof MetaPlan && !(plan.isCheckedOut() || (cv != null && cv.isEditing())))
				return false;
			if (plan instanceof CommonPlan)
				return false;
		} catch (Exception e) {
		}
		return true;
	}
	private boolean isVersionStatus(TbReport m_report) {
		if (m_report != null && m_report instanceof TbReport) {
			return ((TbReport)m_report).getMode() == IMode.VERSION;
		}
		else
			return false;
	}
}
